package Bean;

import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AddUser extends Database {

	private String mUserName = null;
	private String mPassword = null;
	private String mRepasswd = null;
	private String mEmail = null;
	private String mCertifyCodeId = null;
	private String mCodeSession = null;
	private boolean mIsSubmit = false;
	public String mError = "";
	private CheckValue mMD5 = new CheckValue();

	public void setUsername(String username) {
		this.mUserName = username;
	}

	public void setSubmit(String submit) {
		mIsSubmit = (submit != null && submit.equalsIgnoreCase("1"));
	}

	public void setPassword(String password) {
		this.mPassword = password;
	}

	public void setCodeSession(String code) {
		this.mCodeSession = code;
	}

	public void setRepasswd(String password) {
		this.mRepasswd = password;
	}

	public void setRegemail(String email) {
		this.mEmail = email;
	}

	public void setCertifycodeid(String certifycodeid) {
		this.mCertifyCodeId = certifycodeid;
	}

	public int checkUser() {
		int add = 0;

		if (!mIsSubmit) {
			return 1;
		}

		if (mUserName == null || mUserName.isEmpty()) {
			printHtml("username_empty");
			return add;
		}

		if (mPassword == null || mPassword.isEmpty()) {
			printHtml("password_empty");
			return add;
		}

		if (mRepasswd == null || mRepasswd.isEmpty()) {
			printHtml("repasswd_empty");
			return add;
		}

		if (!mRepasswd.equals(mPassword)) {
			printHtml("password_notmatch");
			return add;
		}

		if (mPassword.length() < 6) {
			printHtml("password_short");
			return add;
		}

		if (mEmail == null || mEmail.isEmpty()) {
			printHtml("regemail_empty");
			return add;
		}
		
		if (!mMD5.isEmail(mEmail)) {
			printHtml("regemail_invalid");
			return add;
		}

		if (mCertifyCodeId == null || mCertifyCodeId.isEmpty() || mCodeSession == null || mCodeSession.isEmpty()) {
			printHtml("certifycode_empty");
			return add;
		}

		if (!isCertifyCode(mCertifyCodeId)) {
			printHtml("certifycode");
			return add;
		}

		String sql = "SELECT * FROM members WHERE username = '" + mUserName + "'";

		try {
			mRs = mStmt.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (mRs.next()) {
				printHtml("username_existed");
				add =  0;
				return add;
			} else {
				add = 2;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		sql = "SELECT * FROM members WHERE email = '" + mEmail + "'";

		try {
			mRs = mStmt.executeQuery(sql);
			
			if (mRs.next()) {
				printHtml("regemail_existed");
				add = 0;
			} else {
				add = 2;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return add;
	}

	public int addNewUser() {
		int add = 0;

		String sql = "";

		try {
			sql = "INSERT INTO members (username, password, email, regist_time) VALUES ('"
					   + mUserName + "', '"
					   + mMD5.getMD5(mPassword) + "', '"
					   + mEmail + "', NOW())";
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			mStmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		sql = "SELECT id FROM members WHERE username = '" + mUserName + "'";
		try {
			ResultSet rs = mStmt.executeQuery(sql);
			
			if (rs.next()) {
				add = rs.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		printHtml("success");

		return add;
	}

	private void printHtml(String error) {
		mError = "<html><body><script language=\"javascript\" type=\"text/javascript\">parent.valiregister(\""
		       + error
		       + "\", true);</script></body></html>";
	}

	private boolean isCertifyCode(String str) {
		if(str.equals(mCodeSession)) {
			return true;
		} else {
			return false;
		}
	}
}
